var TOURIST = {
    init: function () {
        this.bindEvent();
        this.bindSelect();
    },
    bindEvent: function () {
        var self = this;
        $('[name = "city"]').on('change',function(){
            self.unbindSelect();
            self.bindSelect();
        })

        $('.business-area-list').on('click', '.fa-remove', function () {
            var span = $(this).parent();
            var id = span.attr('data-id');
            var va = span.attr('data-value');
            span.remove();
            var ids = $('[name = "business_area_ids"]').val() ? $('[name = "business_area_ids"]').val().split(',') : [];
            var strs = $('[name = "business_area"]').val() ? $('[name = "business_area"]').val().split(',') : [];
            if (self.findInArray(ids, id + '') > -1) {
                ids.splice(self.findInArray(ids, id + ''), 1);
                strs.splice(self.findInArray(strs, va), 1);
            }
            $('[name = "business_area_ids"]').val(ids.join(','));
            $('[name = "business_area"]').val(strs.join(','));
        })

        $('.district-list').on('click', '.fa-remove', function () {
            var span = $(this).parent();
            var id = span.attr('data-id');
            var va = span.attr('data-value');
            span.remove();
            var ids = $('[name = "district_ids"]').val() ? $('[name = "district_ids"]').val().split(',') : [];
            var strs = $('[name = "district"]').val() ? $('[name = "district"]').val().split(',') : [];
            if (self.findInArray(ids, id + '') > -1) {
                ids.splice(self.findInArray(ids, id + ''), 1);
                strs.splice(self.findInArray(strs, va), 1);
            }
            $('[name = "district_ids"]').val(ids.join(','));
            $('[name = "district"]').val(strs.join(','));
        })
    },
    bindSelect: function(){
        var self = this;
        $('.search-business-area').autocomplete({
            serviceUrl: 'http://api.zhugefang.com/API/Search/guessword',
            type: 'post',
            params: {
                'token': '27jrhYrFmp_841zOdi2RaYufPzNzZgANRShHjMobgVYVz19bEVB6l3B9_JDHpN4gfS7hM9f2wN6gMKp8r1XQeg==',
                'platformType': 2,
                'city': $('[name="city"] option:selected').val(),
                'type_id': '2',  //1城区  2商圈  3 小区  4 地铁  5 地铁站
                'houseType': 1
            },
            paramName: 'word',
            transformResult: function (response) {
                response = $.parseJSON(response);
                return {
                    suggestions: $.map(response.data, function (dataItem) {
                        return {value: dataItem.keyword, data: dataItem.other_id};
                    })
                };
            },
            onSelect: function (s) {
                var ids = $('[name = "business_area_ids"]').val() ? $('[name = "business_area_ids"]').val().split(',') : [];
                var strs = $('[name = "business_area"]').val() ? $('[name = "business_area"]').val().split(',') : [];
                if (self.findInArray(ids, s.data + '') < 0 && self.findInArray(strs,s.value)) {
                    ids.push(s.data);
                    strs.push(s.value);
                    $('[name = "business_area_ids"]').val(ids.join(','));
                    $('[name = "business_area"]').val(strs.join(','));
                    $('.business-area-list').append('<span class="btn btn-info" data-id="' + s.data + '" data-value="'+s.value+'">' + s.value + '<i class="fa fa-remove" aria-hidden="true"></i></span>');
                    $('.search-business-area').val('');
                }
                return false;
            }
        })

        $('.search-district').autocomplete({
            serviceUrl: 'http://api.zhugefang.com/API/Search/guessword',
            type: 'post',
            params: {
                'token': '27jrhYrFmp_841zOdi2RaYufPzNzZgANRShHjMobgVYVz19bEVB6l3B9_JDHpN4gfS7hM9f2wN6gMKp8r1XQeg==',
                'platformType': 2,
                'city': $('[name="city"] option:selected').val(),
                'type_id': '3',  //1城区  2商圈  3 小区  4 地铁  5 地铁站
                'houseType': 1
            },
            paramName: 'word',
            transformResult: function (response) {
                response = $.parseJSON(response);
                return {
                    suggestions: $.map(response.data, function (dataItem) {
                        return {value: dataItem.keyword, data: dataItem.other_id};
                    })
                };
            },
            onSelect: function (s) {
                var ids = $('[name = "district_ids"]').val() ? $('[name = "district_ids"]').val().split(',') : [];
                var strs = $('[name = "district"]').val() ? $('[name = "district"]').val().split(',') : [];
                if (self.findInArray(ids, s.data + '') < 0) {
                    ids.push(s.data);
                    strs.push(s.value);
                    $('[name = "district_ids"]').val(ids.join(','));
                    $('[name = "district"]').val(strs.join(','));
                    $('.district-list').append('<span class="btn btn-info" data-id="' + s.data + '" data-value="'+s.value+'">' + s.value + '<i class="fa fa-remove" aria-hidden="true"></i></span>');
                    $('.search-district').val('');
                }
                return false;
            }
        })
    },
    unbindSelect : function(){
        $('.search-business-area').autocomplete().dispose();
        $('.search-district').autocomplete().dispose();
    },
    findInArray: function (arr, item) {
        var a = -1;
        if (arr.length) {
            $.each(arr, function (i, o) {
                if (o == item) {
                    a = i;
                    return false;
                }
            })
        }
        return a;
    }
};

TOURIST.init();